package cn.ruanyun.backInterface.modules.business.profitPercent.controller;

import cn.ruanyun.backInterface.common.utils.PageUtil;
import cn.ruanyun.backInterface.common.utils.ResultUtil;
import cn.ruanyun.backInterface.common.utils.ToolUtil;
import cn.ruanyun.backInterface.common.vo.PageVo;
import cn.ruanyun.backInterface.common.vo.Result;
import cn.ruanyun.backInterface.modules.base.pojo.DataVo;
import cn.ruanyun.backInterface.modules.business.profitPercent.pojo.ProfitPercent;
import cn.ruanyun.backInterface.modules.business.profitPercent.service.IProfitPercentService;
import cn.ruanyun.backInterface.modules.business.profitPercent.vo.ProfitPercentVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author z
 * 分红比例管理接口
 */
@Slf4j
@RestController
@RequestMapping("/ruanyun/profitPercent")
@Transactional
@Api(tags = "分红比例")
public class ProfitPercentController {

    @Autowired
    private IProfitPercentService iProfitPercentService;


    @PostMapping(value = "/insertOrderUpdateProfitPercent")
    @ApiOperation("更新或者插入数据")
    public Result<Object> insertOrderUpdateProfitPercent(ProfitPercent profitPercent){

        //不可创建相同分类分佣比例

        if (ToolUtil.isEmpty(profitPercent.getId())) {

            List<ProfitPercent> list = iProfitPercentService.list(Wrappers.<ProfitPercent>lambdaQuery()
                    .eq(ProfitPercent::getCategoryId, profitPercent.getCategoryId()));

            if (list.size() > 0){

                return new ResultUtil<>().setErrorMsg(208,"该分类已添加分红比例!");

            }
        }
        iProfitPercentService.insertOrderUpdateProfitPercent(profitPercent);
        return new ResultUtil<>().setSuccessMsg("插入或者更新成功!");
    }

    @PostMapping(value = "/removeProfitPercent")
    @ApiOperation("移除数据")
    public Result<Object> removeProfitPercent(String ids){

        iProfitPercentService.removeProfitPercent(ids);
        return new ResultUtil<>().setSuccessMsg("移除成功！");
    }


    @PostMapping("/getProfitPercentList")
    @ApiOperation("获取分红列表")
    public Result<DataVo<ProfitPercentVo>> getProfitPercent(PageVo pageVo, String categoryId) {

        return iProfitPercentService.getProfitPercent(pageVo, categoryId);
    }

}
